<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
date_default_timezone_set ( 'prc' );
class User extends CI_Controller {
	public function __construct() {
		parent::__construct ();
	}
	/*
	 * 跳转到个人中心页面
	 */
	public function index() {
		$userid = $this->session->userdata ( 'id' );
		if (empty($userid)) {
			redirect ( 'passport/index' );
		}
		$this->load->model ( 'user_model' );
		$user = $this->user_model->load ( $userid );
		$this->load->model ( 'school_model' );
		$class = $this->school_model->find_class_by_id ($user['class']);
		$schools = $this->school_model->get_schools ();
		$grades = $this->school_model->find_grade_by_type ($class['type']);
		$classes = $this->school_model->find_class_by_school_grade ($class['schoolid'],$class['gradeid']);
		$data['user'] = $user;
		$data['school'] = $schools;
		$data['grade'] = $grades;
		$data['class'] = $classes;
		$this->load->view ( 'user/index', $data);
	}
	/*
	 * 跳转到编辑资料页面
	 */
	public function edit() {
		$userid = $this->session->userdata ( 'id' );
		if (empty($userid)) {
			redirect ( 'passport/index' );
		}
		$this->load->model ( 'user_model' );
		$user = $this->user_model->load ( $userid );
		$this->load->model ( 'school_model' );
		$class = $this->school_model->find_class_by_id ($user['class']);
		$schools = $this->school_model->get_schools ();
		$grades = $this->school_model->find_grade_by_type ($class['type']);
		$classes = $this->school_model->find_class_by_school_grade ($class['schoolid'],$class['gradeid']);
		$data['user'] = $user;
		$data['school'] = $schools;
		$data['grade'] = $grades;
		$data['class'] = $classes;
		$this->load->view ( 'user/edit', $data);
	}
	/*
	 * 更新用户资料
	 */
	public function update() {
		$userid = $this->session->userdata ( 'id' );
		if(empty($userid)){
			return_json ( FALSE, '', '请重新登录后再操作！' );
		}
		$user = array (
			'pname' => $this->input->post ( 'pname' ),
			'sname' => $this->input->post ( 'sname' ),
			'class' => $this->input->post ( 'class' ),
			'sno' => $this->input->post ( 'sno' )
		);
		$this->load->model ( 'user_model' );
		if ($this->user_model->update ( $userid, $user )) {
			return_json(TRUE,'','修改成功!');
		}else{
			return_json(FALSE,'','信息未变动!');
		}
	}
	/*
	 * 更新用户密码
	 */
	public function updatePassword() {
		$userid = $this->session->userdata ( 'id' );
		if(empty($userid)){
			return_json ( FALSE, '', '请重新登录后再操作！' );
		}
		$this->load->model ( 'user_model' );
		$user=$this->user_model->load ( $userid );
		
		$oldpassword = $this->input->post ( 'oldpassword' );
		$newpassword = $this->input->post ( 'newpassword' );
		if(md5($oldpassword.'jishibao')!=$user['password']){
			return_json(FALSE,'','旧密码错误!');
		}
		$newuser = array (
			'password' => md5($newpassword.'jishibao')
		);
		if ($this->user_model->update ( $userid, $newuser )) {
			return_json(TRUE,'','修改密码成功!');
		}else{
			return_json(FALSE,'','密码没有变化!');
		}
	}
	/**
	 * 跳转到营养分析页面
	 */
	public function nutrient() {
		if (! $this->session->userdata ( 'logged_in' )) {
			redirect ( 'passport/index' );
			exit ();
		}
		$userid = $this->session->userdata ( 'id' );
		$this->load->model ( 'user_model' );
		$user = $this->user_model->load ( $userid );
		$this->load->model ( 'school_model' );
		$data['user'] = $user;
		$this->load->view ( 'user/nutrient', $data);
	}
	/*
	 *返回营养统计
	 */
	public function getNutrient() {
		$userid = $this->session->userdata ( 'id' );
		//$userid = 278;
		$startdate = $this->input->post( 'startdate' );
		$enddate = $this->input->post( 'enddate' );
		$this->load->model ( 'user_model' );
		$this->load->model ( 'order_model' );
		$this->load->model ( 'compose_model' );
		$user = $this->user_model->load ( $userid );
		$danbaizhi=0;
		$zhifang=0;
		$tanshui=0;
		$nengliang=0;
		$day=0;
		for($start = strtotime($startdate); $start <= strtotime($enddate);$start += 3600*24){
			$temp=$this->order_model->find_by_date($userid,date ( "Y-m-d", $start) );
			if(!empty($temp)){//当天是否订餐
				$danbaizhi+=$temp['danbaizhi'];
				$zhifang+=$temp['zhifang'];
				$tanshui+=$temp['tanshui'];
				$nengliang+=$temp['nengliang'];
				$day++;
			}
		}
		if($day==0){//日期内没有订餐
			$nutrient['danbaizhi']=0;
			$nutrient['zhifang']=0;
			$nutrient['tanshui']=0;
			$nutrient['nengliang']=0;
		}else{
			$nutrient['danbaizhi']=round($danbaizhi/$day,2);
			$nutrient['zhifang']=round($zhifang/$day,2);
			$nutrient['tanshui']=round($tanshui/$day,2);
			$nutrient['nengliang']=round($nengliang/$day,2);
		}
		$data['nutrient']=$nutrient;
		$age=0;
		$gender=0;
		if(empty($user['age'])){
			$age=10;
		}
		if(empty($user['gender'])){
			$gender=0;
		}
		$standard=$this->compose_model->get_standard_nutrition($age,$gender);
		$data['standard']=$standard;
		return_json_data($data);
	}
}
